/**
 * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 */

package de.hsmannheim.pepa.patientenportal.apanwep.model;

import com.liferay.portal.kernel.bean.AutoEscapeBeanHandler;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.util.ProxyUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.model.BaseModel;
import com.liferay.portal.model.impl.BaseModelImpl;

import de.hsmannheim.pepa.patientenportal.apanwep.service.ClpSerializer;
import de.hsmannheim.pepa.patientenportal.apanwep.service.participantsLocalServiceUtil;

import java.io.Serializable;

import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Cedric Nana
 */
public class participantsClp extends BaseModelImpl<participants>
	implements participants {
	public participantsClp() {
	}

	@Override
	public Class<?> getModelClass() {
		return participants.class;
	}

	@Override
	public String getModelClassName() {
		return participants.class.getName();
	}

	@Override
	public long getPrimaryKey() {
		return _ID;
	}

	@Override
	public void setPrimaryKey(long primaryKey) {
		setID(primaryKey);
	}

	@Override
	public Serializable getPrimaryKeyObj() {
		return _ID;
	}

	@Override
	public void setPrimaryKeyObj(Serializable primaryKeyObj) {
		setPrimaryKey(((Long)primaryKeyObj).longValue());
	}

	@Override
	public Map<String, Object> getModelAttributes() {
		Map<String, Object> attributes = new HashMap<String, Object>();

		attributes.put("ID", getID());
		attributes.put("Userid", getUserid());
		attributes.put("Username", getUsername());

		return attributes;
	}

	@Override
	public void setModelAttributes(Map<String, Object> attributes) {
		Long ID = (Long)attributes.get("ID");

		if (ID != null) {
			setID(ID);
		}

		String Userid = (String)attributes.get("Userid");

		if (Userid != null) {
			setUserid(Userid);
		}

		String Username = (String)attributes.get("Username");

		if (Username != null) {
			setUsername(Username);
		}
	}

	@Override
	public long getID() {
		return _ID;
	}

	@Override
	public void setID(long ID) {
		_ID = ID;

		if (_participantsRemoteModel != null) {
			try {
				Class<?> clazz = _participantsRemoteModel.getClass();

				Method method = clazz.getMethod("setID", long.class);

				method.invoke(_participantsRemoteModel, ID);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getUserid() {
		return _Userid;
	}

	@Override
	public void setUserid(String Userid) {
		_Userid = Userid;

		if (_participantsRemoteModel != null) {
			try {
				Class<?> clazz = _participantsRemoteModel.getClass();

				Method method = clazz.getMethod("setUserid", String.class);

				method.invoke(_participantsRemoteModel, Userid);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	@Override
	public String getUsername() {
		return _Username;
	}

	@Override
	public void setUsername(String Username) {
		_Username = Username;

		if (_participantsRemoteModel != null) {
			try {
				Class<?> clazz = _participantsRemoteModel.getClass();

				Method method = clazz.getMethod("setUsername", String.class);

				method.invoke(_participantsRemoteModel, Username);
			}
			catch (Exception e) {
				throw new UnsupportedOperationException(e);
			}
		}
	}

	public BaseModel<?> getparticipantsRemoteModel() {
		return _participantsRemoteModel;
	}

	public void setparticipantsRemoteModel(BaseModel<?> participantsRemoteModel) {
		_participantsRemoteModel = participantsRemoteModel;
	}

	public Object invokeOnRemoteModel(String methodName,
		Class<?>[] parameterTypes, Object[] parameterValues)
		throws Exception {
		Object[] remoteParameterValues = new Object[parameterValues.length];

		for (int i = 0; i < parameterValues.length; i++) {
			if (parameterValues[i] != null) {
				remoteParameterValues[i] = ClpSerializer.translateInput(parameterValues[i]);
			}
		}

		Class<?> remoteModelClass = _participantsRemoteModel.getClass();

		ClassLoader remoteModelClassLoader = remoteModelClass.getClassLoader();

		Class<?>[] remoteParameterTypes = new Class[parameterTypes.length];

		for (int i = 0; i < parameterTypes.length; i++) {
			if (parameterTypes[i].isPrimitive()) {
				remoteParameterTypes[i] = parameterTypes[i];
			}
			else {
				String parameterTypeName = parameterTypes[i].getName();

				remoteParameterTypes[i] = remoteModelClassLoader.loadClass(parameterTypeName);
			}
		}

		Method method = remoteModelClass.getMethod(methodName,
				remoteParameterTypes);

		Object returnValue = method.invoke(_participantsRemoteModel,
				remoteParameterValues);

		if (returnValue != null) {
			returnValue = ClpSerializer.translateOutput(returnValue);
		}

		return returnValue;
	}

	@Override
	public void persist() throws SystemException {
		if (this.isNew()) {
			participantsLocalServiceUtil.addparticipants(this);
		}
		else {
			participantsLocalServiceUtil.updateparticipants(this);
		}
	}

	@Override
	public participants toEscapedModel() {
		return (participants)ProxyUtil.newProxyInstance(participants.class.getClassLoader(),
			new Class[] { participants.class }, new AutoEscapeBeanHandler(this));
	}

	@Override
	public Object clone() {
		participantsClp clone = new participantsClp();

		clone.setID(getID());
		clone.setUserid(getUserid());
		clone.setUsername(getUsername());

		return clone;
	}

	@Override
	public int compareTo(participants participants) {
		long primaryKey = participants.getPrimaryKey();

		if (getPrimaryKey() < primaryKey) {
			return -1;
		}
		else if (getPrimaryKey() > primaryKey) {
			return 1;
		}
		else {
			return 0;
		}
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}

		if (!(obj instanceof participantsClp)) {
			return false;
		}

		participantsClp participants = (participantsClp)obj;

		long primaryKey = participants.getPrimaryKey();

		if (getPrimaryKey() == primaryKey) {
			return true;
		}
		else {
			return false;
		}
	}

	@Override
	public int hashCode() {
		return (int)getPrimaryKey();
	}

	@Override
	public String toString() {
		StringBundler sb = new StringBundler(7);

		sb.append("{ID=");
		sb.append(getID());
		sb.append(", Userid=");
		sb.append(getUserid());
		sb.append(", Username=");
		sb.append(getUsername());
		sb.append("}");

		return sb.toString();
	}

	@Override
	public String toXmlString() {
		StringBundler sb = new StringBundler(13);

		sb.append("<model><model-name>");
		sb.append(
			"de.hsmannheim.pepa.patientenportal.apanwep.model.participants");
		sb.append("</model-name>");

		sb.append(
			"<column><column-name>ID</column-name><column-value><![CDATA[");
		sb.append(getID());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>Userid</column-name><column-value><![CDATA[");
		sb.append(getUserid());
		sb.append("]]></column-value></column>");
		sb.append(
			"<column><column-name>Username</column-name><column-value><![CDATA[");
		sb.append(getUsername());
		sb.append("]]></column-value></column>");

		sb.append("</model>");

		return sb.toString();
	}

	private long _ID;
	private String _Userid;
	private String _Username;
	private BaseModel<?> _participantsRemoteModel;
}